/**************************************
所有类中的成员函数的具体实现
**************************************/

#include "classes.h"


/**********************************************************************************
管理类的成员函数实现
**********************************************************************************/
WorkerManager::WorkerManager()
{
    //判断文件是否存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    //文件不存在的情况
    if (!ifs.is_open())
    {
        //cout << "文件不存在" << endl;
        //初始化属性
        this->m_empnum = 0;//初始化记录人数
        this->m_emp_array = NULL;//初始化数组指针
        this->m_file_is_empty = true;//初始化文件为空
        ifs.close();
    }
    //文件存在但是数据为空的情况
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        //cout << "文件为空" << endl;
        //初始化属性
        this->m_empnum = 0;//初始化记录人数
        this->m_emp_array = NULL;//初始化数组指针
        this->m_file_is_empty = true;//初始化文件为空
        ifs.close();
    }
    //文件存在并且记录了数据
    int num = this->get_empnum();
    //cout << "职工人数为: " << num << endl;
    this->m_empnum = num;
    this->m_emp_array = new Worker*[this->m_empnum];
    // 将文件中的数据存到数组中
    this->init_emp();
    //for (int i = 0; i < this->m_empnum; i++)
    //{
    //    cout << "职工编号: " << this->m_emp_array[i]->m_id << endl;
    //}
    
}
WorkerManager::~WorkerManager()//释放堆区数据
{
    if (this->m_emp_array != NULL)
    {
        delete [] this->m_emp_array;
        this->m_emp_array = NULL;
    }
}
//1.显示菜单信息
void WorkerManager::show_menu()
{
    cout << "======== 欢迎使用职工管理系统 ========" << endl;
    cout << "========== 0.退出管理程序 ===========" << endl;
    cout << "========== 1.增加职工信息 ===========" << endl;
    cout << "========== 2.显示职工信息 ===========" << endl;
    cout << "========== 3.删除离职员工 ===========" << endl;
    cout << "========== 4.修改职工信息 ===========" << endl;
    cout << "========== 5.查找职工信息 ===========" << endl;
    cout << "========== 6.按照编号排序 ===========" << endl;
    cout << "========== 7.清空所有记录 ===========" << endl;
    cout << endl;
}

//2.实现退出系统功能
void WorkerManager::exit_system()
{
    cout << "欢迎下次使用" << endl;
    exit(0);//退出程序
}
//3. 实现添加员工信息的功能
//3.1 保存文件的实现
void WorkerManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out);
    //将每个人的数据写入到文件中
    for (int i=0; i < this->m_empnum; i++)
    {
        ofs << this->m_emp_array[i]->m_id;
        ofs << "\t";
        ofs << this->m_emp_array[i]->m_name;
        ofs << "\t";
        ofs << this->m_emp_array[i]->m_departid << endl;
    }
    ofs.close();
}
//3.2 添加新员工并保存到指定文件中
void WorkerManager::add_emp()
{
    cout << "请输入添加员工的数量: ";
    int add_num = 0;
    cin >> add_num;
    if (add_num > 0)
    {
        //添加新的员工信息
        //首先计算添加新空间的大小
        int newsize = this->m_empnum + add_num;
        //在堆区开辟新空间
        Worker** newspace= new Worker*[newsize];
        //将原来空间下的数据拷贝到新空间下
        if (this->m_emp_array != NULL)
        {
            for (int i = 0; i < this->m_empnum; i++)
            {
                newspace[i] = this->m_emp_array[i];
            }
        }
        //批量添加新数据
        for (int i = 0; i < add_num; i++)
        {
            int id;//职工编号
            string name;//职工姓名
            int depart_select;//部门选择
            cout << "请输入第 " << i+1 << " 个新员工的编号: ";
            cin >> id;
            int temp = this->is_emp_exit(id);
            while (temp != -1)
            {

                cout << "员工编号已存在, 请重新输入: ";
                cin >> id;
                temp = this->is_emp_exit(id);
            }
            cout << "请输入第 " << i+1 << " 个新员工的姓名: ";
            cin >> name;
            cout << "请选择该职工岗位: " << endl;
            cout << "1.普通职员" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
            cin >> depart_select;

            Worker* worker = NULL;
            switch (depart_select)
            {
                case 1:
                    worker = new Employee(id, name, 1);
                    break;
                case 2:
                    worker = new Manager(id,name, 2);
                    break;
                case 3:
                    worker = new Boss(id,name, 3);
                    break;
                default:
                    break;
            }
            //将创建职工保存到数组中
            newspace[this->m_empnum + i] = worker;
        }
        //释放原来在堆区开辟的存放work*的数组空间
        delete [] this->m_emp_array;
        //更改新空间的指向
        this->m_emp_array = newspace;
        //更新职工人数
        this->m_empnum = newsize;
        //更新职工不为空标志
        this->m_file_is_empty = false;
        //成功添加之后保存到磁盘中
        this->save();
        //提示添加成功
        cout << "成功添加" << add_num << "名新员工" << endl;
    }
    else
    {
        cout << "输入有误" << endl;
    }
    //system("pause");
    //system("cls");
}

//统计文件中人数
int WorkerManager::get_empnum()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);//打开文件
    
    int id;
    string name;
    int depart_id;

    int num = 0;
    while (ifs >> id && ifs >> name && ifs >> depart_id)
    {
        num++;//统计人数的变量
    }
    return num;
}

//初始化员工信息--将文件中的数据加载到内存中
void WorkerManager::init_emp()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    int id;
    string name;
    int depart_id;
    int index = 0;
    while (ifs >> id && ifs >> name && ifs >> depart_id)
    {
        Worker* worker = NULL;
        if (depart_id == 1)//普通员工
        {
            worker = new Employee(id, name, depart_id);
        }
        else if (depart_id == 2)//经理
        {
            worker = new Manager(id, name, depart_id);
        }
        else//老板
        {
            worker = new Boss(id, name, depart_id);
        }
        this->m_emp_array[index] = worker;
        index++;
    }
    ifs.close();
}

void WorkerManager::show_emp()//显示职工信息
{
    //判断文件是否为空
    if (this->m_file_is_empty)
    {
        cout << "文件不存在或者为空" << endl;
    }
    else
    {
        //利用多态调用程序接口
        for (int i = 0; i < m_empnum; i++)
        {
            this->m_emp_array[i]->show_info();
        }
    }
    //system("pause");
    //system("cls");
}

//删除职工
void WorkerManager::delete_emp()
{
    if (this->m_file_is_empty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        //按照职工编号进行删除
        cout << "请输入想要删除的员工的职工编号: ";
        int id;
        cin >> id;
        int index = this->is_emp_exit(id);
        if (index != -1)//说明职工存在，并且要删除index位置上的职工
        {
            //将数据前移
            for (int i = index; i < this->m_empnum-1; i++)
            {
                this->m_emp_array[i] = this->m_emp_array[i+1];
            }
            this->m_empnum--;//更新数组中记录的人员个数
            this->save();//将数据同步更新到文件中
            cout << "删除成功" << endl;
        }
        else
        {
            cout << "删除失败，未找到该员工" << endl;
        }
    }
    //system("pause");
    //system("cls");
}

//根据职工信息判断某个员工是否存在，并返回职工在数组中的位置，不存在返回-1
int WorkerManager::is_emp_exit(int id)
{
    int index = -1;
    for (int i = 0; i < this->m_empnum; i++)
    {
        if (this->m_emp_array[i]->m_id == id)
        {
            index = i;
            break;
        }
    }
    return index;
}

void WorkerManager::modify_emp()
{
    if (this->m_file_is_empty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "请输入想要修改的员工编号: ";
        int id;
        cin >> id;
        int ret = this->is_emp_exit(id);
        if (ret != -1)
        {
            //查找到指定编号的员工
            delete this->m_emp_array[ret];
            int new_id = 0;
            string new_name = "";
            int new_depart_id = 0;

            cout << "请输入该员工新的员工编号: ";
            cin >> new_id;
            int index = this->is_emp_exit(new_id);
            while (index != -1)
            {
                cout << "您输入的职员编号已存在, 请重新输入: ";
                cin >> new_id;
                index = this->is_emp_exit(new_id);
            }

            cout << "请输入该员工新的姓名: ";
            cin >> new_name;

            cout << "请输入该员工岗位信息: " << endl << "1.普通员工\t" << "2.经理\t" << "3.老板" << endl;
            cin >> new_depart_id;

            Worker* worker = NULL;
            switch (new_depart_id)
            {
                case 1:
                    worker = new Employee(new_id, new_name, new_depart_id);
                    break;
                case 2:
                    worker = new Manager(new_id, new_name, new_depart_id);
                    break;
                case 3:
                    worker = new Boss(new_id, new_name, new_depart_id);
                    break;
                default:
                    break;
            }
            this->m_emp_array[ret] = worker;//更新数据到数组中
            cout << "修改成功" << endl;
            this->save();//保存到文件中
        }
        else
        {
            cout << "修改失败, 查无此人" << endl;
        }
    }
    //system("pause");
    //system("cls");
}



//查找职工信息
void WorkerManager::find_emp()
{
    if (this->m_file_is_empty)
    {
        cout << "文件不存在或者记录为空" << endl;
    }
    else
    {
        cout << "请输入查找的方式: " << endl;
        cout << "1.按照职工编号查找\t" << "2.按照职工姓名查找" << endl;
        int select = 0;
        cin >> select;
        while (true)
        {
            if (select == 1 || select == 2)
            {
                break;
            }
            else
            {
                cout << "输入有误, 请重新输入" << endl;
                cout << "1.按照职工编号查找\t" << "2.按照职工姓名查找" << endl;
                cin >> select;
            }
        }
        if (select == 1)
        {
            //按照编号查询
            int id;
            cout << "请输入职工编号: ";
            cin >> id;
            int ret= this->is_emp_exit(id);
            if (ret != -1)
            {
                cout << "查找成功, 该职员信息如下: " << endl;
                this->m_emp_array[ret]->show_info();
            }
            else
            {
                cout << "查无此人" << endl;
            }
        }
        else if (select == 2)
        {
            cout << "请输入查找的姓名: ";
            string name;
            cin >> name;
            bool flag = false;
            for (int i = 0; i < this->m_empnum; i++)
            {
                if (this->m_emp_array[i]->m_name == name)
                {
                    cout << "查找成功, 详细信息如下: " << endl;
                    break;
                }
            }
            for (int i = 0; i < this->m_empnum; i++)
            {
                if (this->m_emp_array[i]->m_name == name)
                {
                    this->m_emp_array[i]->show_info();
                    flag = true;
                }
            }
            if (flag == false)
            {
                cout << "查无此人" << endl;
            }
        }
    }
}

//按照编号进行排序
void WorkerManager::sort_emp()
{
    if (this->m_file_is_empty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "请选择排序方式" << endl;
        cout << "1.按职工编号进行升序排列" << endl;
        cout << "2.按职工编号进行降序排列" << endl;
        int select = 0;
        cin >> select;
        while (true)
        {
            if (select == 1 || select == 2)
            {
                break;
            }
            else
            {
                cout << "输入有误, 请重新输入" << endl;
                cout << "1.按职工编号进行升序排列" << endl;
                cout << "2.按职工编号进行降序排列" << endl;
                cin >> select;
            }
        }
        for (int i = 0; i < m_empnum - 1; i++)
        {
            int min_or_max = i;//声明最小值或者最大值的下标
            for (int j = i + 1; j < this->m_empnum; j++)
            {
                if (select == 1)//升序
                {
                    if (this->m_emp_array[min_or_max]->m_id > this->m_emp_array[j]->m_id)
                    {
                        min_or_max = j;
                    }
                }
                else//降序
                {
                    if (this->m_emp_array[min_or_max]->m_id < this->m_emp_array[j]->m_id)
                    {
                        min_or_max = j;
                    }
                }
            }
            //判断一开始认定的最大值或最小值下标是不是计算的最小值或者最大值，如果不是，交换数据
            if (i != min_or_max)
            {
                Worker* temp = this->m_emp_array[i];
                this->m_emp_array[i] = this->m_emp_array[min_or_max];
                this->m_emp_array[min_or_max] = temp;
            }
        }
        cout << "排序成功, 排序后的结果为: " << endl;
        this->show_emp();
        cout << "排序结果是否同步更新到文件中\n1.是\t2.否" << endl;
        int choose = 0;
        cin >> choose;
        while (true)
        {
            if (choose == 1)
            {
                this->save();//保存结果
                cout << "排序结果成功保存到文件中" << endl;
                break;
            }
            else if (choose == 2)
            {
                cout << "排序结果未保存到文件中" << endl;
                break;
            }
            else
            {
                cout << "输入有误, 请输入正确的选项" << endl;
                cout << "1.同步排序结果\t2.放弃排序结果" << endl;
                cin >> choose;
            }
        }
    }
}

//清空文件
void WorkerManager::clean_emp()
{
    cout << "确认清空? " << endl;
    cout << "1.确认" << endl;
    cout << "2.返回" << endl;
    int select = 0;
    cin >> select;
    while (true)
    {
        if (select == 1 || select == 2)
        {
            break;
        }
        else
        {
            cout << "输入有误, 请重新输入" << endl;
            cin >> select;
        }
    }
    if (select == 1)
    {
        //清空文件
        ofstream ofs;
        ofs.open(FILENAME, ios::trunc);//删除文件后重新创建
        ofs.close();
        if (this->m_emp_array != NULL)
        {
            for (int i =0; i < this->m_empnum; i++)//删除堆区的每个职工对象
            {
                delete this->m_emp_array[i];
                this->m_emp_array[i] = NULL;
            }
            //删除堆区数组指针
            delete[] this->m_emp_array;
            this->m_emp_array = NULL;
            this->m_empnum = 0;
            this->m_file_is_empty = true;
        }
        cout << "清空成功" << endl;
    }
    //system("pause");
    //system("cls");
}

/****************************************************************************
各个职工类相关成员函数的实现
*****************************************************************************/
//1.普通员工类成员函数实现
Employee::Employee(int id, string name, int depart_id)//构造函数初始化员工信息
{
    this->m_id = id;
    this->m_name = name;
    this->m_departid = depart_id;
}

void Employee::show_info()//显示个人信息
{
    cout << "职工编号: \t" << this->m_id << "\t"
         << "职工姓名: \t" << this->m_name << "\t"
         << "职工岗位: \t" << this->get_depart_name() << "\t"
         << "岗位职责: 完成经理交给的任务" << endl; 
}

string Employee::get_depart_name()
{
    return string("员工");
}

//2. 经理类成员函数的实现
Manager::Manager(int id, string name, int depart_id)//构造函数
{
    this->m_id = id;
    this->m_name = name;
    this->m_departid = depart_id;
}
void Manager::show_info()//显示个人信息
{
    cout << "职工编号: \t" << this->m_id << "\t"
         << "职工姓名: \t" << this->m_name << "\t"
         << "职工岗位: \t" << this->get_depart_name() << "\t"
         << "岗位职责: 完成老板交给的任务, 并下发任务给普通员工" << endl;
}
string Manager::get_depart_name()//获取岗位信息
{
    return string("经理");
}

//3.老板类的成员函数实现
Boss::Boss(int id, string name, int depart_id)//构造函数
{
    this->m_id = id;
    this->m_name = name;
    this->m_departid = depart_id;
}
void Boss::show_info()//显示个人信息
{
    cout << "职工编号: \t" << this->m_id << "\t"
         << "职工姓名: \t" << this->m_name << "\t"
         << "职工岗位: \t" << this->get_depart_name() << "\t"
         << "岗位职责: 管理公司所有事务" << endl;
}
string Boss::get_depart_name()//获取岗位信息
{
    return string("总裁");
}